package com.fieldchain.ex.fa100.mvp.base;

import android.content.Context;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/3
 */
public class MyBasePresenter<T extends MyBaseModel, V extends MyBaseView> {
    public Context mContext;
    public T model;
    public V view;

    public MyBasePresenter() {
        Class c = getClass();
        Type types = c.getGenericSuperclass();
        if (types.toString().contains("<")) {
            Type[] actualTypeArguments = ((ParameterizedType) types).getActualTypeArguments();
            Class<T> tClass = (Class<T>) actualTypeArguments[0];
            try {
                model = tClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public void attachView(V mvpView, Context mContext) {
        this.view = mvpView;
        this.mContext = mContext;
    }

    public void detachView() {
        this.view = null;
        mContext = null;
        model.detachView();
        model = null;
    }

}
